package org.desktop.web.client.custom.widget;

import org.desktop.web.client.DesktopViewer;
import org.desktop.web.client.custom.form.LoginForm;
import org.desktop.web.client.login.service.LoginService;
import org.desktop.web.client.login.service.LoginServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.Dialog;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.PasswordField;
import com.sencha.gxt.widget.core.client.form.TextField;

public class LoginWidget extends Dialog {

	ContentPanel panel=new ContentPanel();
	private DesktopViewer desktopViewer;

	public LoginWidget(DesktopViewer desktopViewer) {
		// TODO Auto-generated constructor stub
		this.desktopViewer = desktopViewer;
		setWidth(300);
		setHeight(200);
		setHeaderVisible(true);
		setHeadingText("Login");
		setClosable(false);
		setModal(true);
		setDraggable(false);
		setClosable(false);
		this.add(getWidget());
		show();

	}

	@Override
	public Widget getWidget() {
			panel.setHeaderVisible(false);
			this.setPredefinedButtons(PredefinedButton.OK,PredefinedButton.CANCEL,PredefinedButton.NO);
			
			setButtonAlign(BoxLayoutPack.CENTER);
			final LoginForm form = new LoginForm(desktopViewer);
			form.setId("login");
			// form.setAction(GWT.getModuleBaseURL()+"exec?service="+WebService.AUTH);
			// form.setMethod(Method.POST);
			panel.add(form);

			VerticalLayoutContainer vlc = new VerticalLayoutContainer();
			form.add(vlc);

			TextField username = new TextField();
			username.setAllowBlank(false);
			username.setName("username");
			username.setId("username");
			vlc.add(new FieldLabel(username, "Username"),
					new VerticalLayoutData(-18, -1));

			PasswordField pwd = new PasswordField();
			pwd.setAllowBlank(false);
			pwd.setName("password");
			pwd.setId("password");
			vlc.add(new FieldLabel(pwd, "Password"), new VerticalLayoutData(
					-18, -1));
			
			TextButton btnSubmit = this.getButton(PredefinedButton.OK);
			btnSubmit.setText("Login");
			btnSubmit.addSelectHandler(new SelectHandler() {

				@Override
				public void onSelect(SelectEvent event) {
					if (!form.isValid()) {
						return;
					}

					// normally would submit the form but for example no server
					// set up to
					// handle the post
					// panel.submit();

					form.submit();

				}
			});

			TextButton btnReset = this.getButton(PredefinedButton.CANCEL);
			btnReset.setText("Annulla");
			btnReset.addSelectHandler(new SelectHandler() {

				@Override
				public void onSelect(SelectEvent event) {
					form.reset();
					// TODO needs to be part of form panel, ie a Field interface
				}
			});
			final TextField tempUserName = username;
			TextButton btnPwdRecover = this.getButton(PredefinedButton.NO);
			btnPwdRecover.setText("Recupera Password");
			btnPwdRecover.addSelectHandler(new SelectHandler() {

				@Override
				public void onSelect(SelectEvent event) {
					if(tempUserName.getText().trim().equals("")){
						AlertMessageBox amb = new AlertMessageBox("Error", "Inserire il nome utente per recuperare la password!");
						amb.setModal(true);
						amb.show();
						return;
					}
					LoginServiceAsync loginService =  GWT.create(LoginService.class);
					loginService.resetPassword(tempUserName.getText(), GWT.getModuleBaseURL()+"DesktopViewer.html",new AsyncCallback<Boolean>() {
						
						@Override
						public void onSuccess(Boolean recover) {
							// TODO Auto-generated method stub
							if(!recover){
								AlertMessageBox amb = new AlertMessageBox("Error", "Recover failed");
								amb.setModal(true);
								amb.show();
								return;
							}
						}
						
						@Override
						public void onFailure(Throwable arg0) {
							// TODO Auto-generated method stub
							
						}
					});
					
					// TODO needs to be part of form panel, ie a Field interface
				}
			});
			



			panel.addButton(btnSubmit);
			panel.addButton(btnReset);
			
			panel.addButton(btnPwdRecover);

		// TODO Auto-generated method stub
//		addResizeHandler(new ResizeHandler() {
//			
//			@Override
//			public void onResize(ResizeEvent event) {
//				center();
//				
//			}
//		});
		return panel;
	}

}
